package sessionbean.facade;

import entity.Price;
import java.math.BigDecimal;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class PriceFacade extends AbstractFacade<Price> {
    @PersistenceContext(unitName = "AmazonWishlistTrackerPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public PriceFacade() {
        super(Price.class);
    }
    
    public BigDecimal findMostRecentPrice(int itemId) {
        Query query = getEntityManager().createNamedQuery("Price.findMostRecentPriceForItem", Price.class);
        query.setParameter("itemId", itemId);
        try {
            return ((Price) query.setMaxResults(1).getSingleResult()).getPrice();
        } catch (NoResultException exception) {
            return null;
        }
    }
}
